// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import $ from 'jquery'
import Vue from 'vue'
import router from './router'
import apiUrl from './apiUrl'
import request from './request'
import amAlert from './components/common/AMAlert.vue'
import amConfirm from './components/common/AMConfirm.vue'
import amModal from './components/common/AMModal.vue'
import utils from './utils'

Vue.config.productionTip = false

window.jquery = $
window.__apiUrl = apiUrl
window.__request = request
window.__appPath = '/'
window.__utils = utils

/* eslint-disable no-new */
window.__app = new Vue({
  router,
  components: { amAlert, amConfirm, amModal },
  data () {
    return {
      alertOption: {
        isShow: false,
        content: '',
        onClose () {
          this.alertOption.isShow = false
        }
      },
      confirmOption: {
        isShow: false,
        content: '',
        onCance () {
          this.confirmOption.isShow = false
        },
        onOK () {
          this.confirmOption.isShow = false
        }
      },
      toastOption: {
        isShow: false,
        type: '',
        text: '',
        onClick () {},
        onClose () {
          this.toastOption.isShow = false
        }
      }
    }
  },
  created () {
    console.log('app created')
  },
  methods: {
    showAlert (opt) {
      const option = {
        isShow: true,
        title: opt.title,
        content: opt.content,
        okText: opt.okText,
        onClose: () => {
          this.alertOption.isShow = false
          if (typeof opt.onClose === 'function') {
            opt.onClose()
          }
        }
      }
      this.alertOption = option
    },
    showToast (opt) {
      const option = {
        isShow: true,
        type: opt.type,
        text: opt.text,
        duration: opt.duration || 1000,
        onClick: opt.onClick || this.toastOption.onClick,
        onClose: () => {
          this.toastOption.isShow = false
          if (typeof opt.onClose === 'function') {
            opt.onClose()
          }
        }
      }
      this.toastOption = option
    },
    showConfirm (opt) {
      const option = {
        isShow: true,
        title: opt.title,
        content: opt.content,
        okText: opt.okText,
        cancelText: opt.cancelText,
        onCancel: () => {
          this.confirmOption.isShow = false
          if (typeof opt.onCancel === 'function') {
            opt.onCancel()
          }
        },
        onOK: () => {
          this.confirmOption.isShow = false
          if (typeof opt.onOK === 'function') {
            opt.onOK()
          }
        }
      }
      this.confirmOption = option
    }
  }
}).$mount('#app')
